import {Game} from "@/Game/Game";
import {ShuffleArray} from "@/Game/CommonMethods";

export class EventClass {
    Name: string;
    Description: string;
    Persistent: boolean;
    Color?: string;
    EffectType: EventEffect;
    EffectStrength?: number;
    TakeEffect?: (game: Game) => void;
    LoseEffect?: (game: Game) => void;
}

export enum EventEffect {
    Consumption,
    ShopIncome,
    CustomerQueue,
    ResolveOneShop,
    Shuffle,
    Levy,
    Junk
}

class FrugalityClass extends EventClass {
    Name = "以德养廉";
    Description = "此牌生效期间所有客人牌被招待结算时消费菜单牌的数量减少 1 张";
    Persistent = true;
    Color = "#e2ceb7";
    EffectType = EventEffect.Consumption;
    EffectStrength = -1;
}

class DiscountClass extends EventClass {
    Name = "无尖不商";
    Description = "此牌生效期间所有店铺牌获得收入时减少 1 两钱";
    Persistent = true;
    Color = "#e8dccd";
    EffectType = EventEffect.ShopIncome;
    EffectStrength = -1;
}

class FestoonClass extends EventClass {
    Name = "张灯结彩";
    Description = "将原有的客人牌队列向右移动到两个加号备用框，然后翻开新的客人牌将客人牌队列补齐为 6 张。此牌生效期间客人牌队列始终保持 6 张。<br/>直到翻开新的事件牌时，将最右边的 2 张客人牌置入客人牌弃牌堆，然后翻开新的客人牌将客人牌队列补齐为 4 张。";
    Persistent = true;
    Color = "#e19a68";
    EffectType = EventEffect.CustomerQueue;
    EffectStrength = 2;

    TakeEffect = (game: Game) => {
        game.commonArea.customerQueueSize = this.EffectStrength + 4;
    };

    LoseEffect = (game: Game) => {
        if (4 < game.commonArea.customerQueue.length) {
            game.commonArea.customerAndEventDiscardPile.push(...game.commonArea.customerQueue.splice(4));
        }
        game.commonArea.customerQueueSize = 4;
    };
}

class DesertedClass extends EventClass {
    Name = "门可罗雀";
    Description = "此牌生效期间所有客人牌被招待结算时玩家只可以选择 1 张店铺牌结算收盆，其他店铺牌（包括同各店铺牌）只有给店铺牌增加收益的效果（茶馆，说书场，关扑铺，首饰铺，饮子铺的联动效果）生效。";
    Persistent = true;
    Color = "#ebd2af";
    EffectType = EventEffect.ResolveOneShop;
}

class BumperHarvestClass extends EventClass {
    Name = "时和年丰";
    Description = "此牌生效期间所有客人牌被招待结算时消费菜单牌的数量增加 1 张";
    Persistent = true;
    Color = "#e9c699";
    EffectType = EventEffect.Consumption;
    EffectStrength = 1;
}

class WinterSnowClass extends EventClass {
    Name = "银装素裹";
    Description = "将最右边的 2 张客人牌置入客人牌弃牌堆，此牌生效期间客人牌队列始终保持 2 张客人牌。<br/>直到翻开新的事件牌时，将原有的客人牌向右侧移动并翻开新的客人牌将客人牌队列补齐为 4 张。";
    Persistent = true;
    Color = "#f8f8f8";
    EffectType = EventEffect.CustomerQueue;
    EffectStrength = -2;

    TakeEffect = (game: Game) => {
        let newQueueSize = this.EffectStrength + 4;
        if (newQueueSize < game.commonArea.customerQueue.length) {
            game.commonArea.customerAndEventDiscardPile.push(...game.commonArea.customerQueue.splice(newQueueSize));
        }
        game.commonArea.customerQueueSize = newQueueSize;
    };

    LoseEffect = (game: Game) => {
        game.commonArea.customerQueueSize = 4;
    };
}

class NewYearClass extends EventClass {
    Name = "辞旧迎新";
    Description = "将所有客人牌（包括客人牌堆、客人牌队列、客人牌弃牌堆）和告示架的事件牌洗混，形成新的客人牌堆置于版图上相应位置。然后重新翻开 4 张客人牌形成客人牌队列，如果此时翻出事件牌则将其置于告示架并结算效果，再翻开新的客人牌将队列补齐。";
    Persistent = false;
    EffectType = EventEffect.Shuffle;
    TakeEffect = (game: Game) => {
        game.commonArea.customerAndEventDiscardPile.push(...game.commonArea.customerAndEventDrawPile.splice(0));
        ShuffleArray(game.commonArea.customerAndEventDiscardPile);
        game.commonArea.customerAndEventDrawPile = [...game.commonArea.customerAndEventDiscardPile];
        game.commonArea.customerAndEventDiscardPile.length = 0;
    };
}

class LevyClass extends EventClass {
    Name = "苛捐杂税";
    Description = "所有玩家每拥有 1 个店铺模型需要支付 2 两钱。玩家支付的钱返回供应堆，如持有钱不足则将持有钱全部支付后不再产生欠款。";
    Persistent = false;
    EffectType = EventEffect.Levy;
    EffectStrength = 2;
    TakeEffect = (game: Game) => {
        for (let index = game.activePlayerIndex; index < game.activePlayerIndex + game.playerAreas.length; index++) {
            let playArea = game.playerAreas[index % game.playerAreas.length];
            let levy = playArea.shopOwned.length * this.EffectStrength;
            if (levy >= playArea.moneyOwned) {
                playArea.moneyOwned = 0;
            }
            else {
                playArea.moneyOwned -= levy;
            }
        }
    };
}

class JunkClass extends EventClass {
    Name = "硕果累累";
    Description = "所有玩家从菜单牌供应堆拿取 2 张四品菜单牌，置入自己面板上的后厨（菜单牌弃牌堆）。";
    Persistent = false;
    EffectType = EventEffect.Junk;
    EffectStrength = 2;
    TakeEffect = (game: Game) => {
        let dishCount = game.commonArea.dishCount;
        for (let index = game.activePlayerIndex; index < game.activePlayerIndex + game.playerAreas.length; index++) {
            let playArea = game.playerAreas[index % game.playerAreas.length];
            if (dishCount[0] > 1) {
                dishCount[0] -= 2;
                playArea.addDishToDiscardPile(0);
                playArea.addDishToDiscardPile(0);
            }
            else if (dishCount[0] == 1) {
                dishCount[0] = 0;
                playArea.addDishToDiscardPile(0);
                break;
            }
        }
    };
}

export const AllEventList: EventClass[] = [new FrugalityClass(), new DiscountClass(), new FestoonClass(), new DesertedClass(), new BumperHarvestClass(), new WinterSnowClass(), new NewYearClass(), new LevyClass(), new JunkClass()];